{
    load_script('ajaxfileupload');
    load_script('jquery.Jcrop.min');

    var image_scale=1;
    var width=0;
    var height=0;
    $('#avt_upload_btn').click(function(){
        if($('#avt_upload_file').val()=='')
        {
            alert('没文件');
            return;
        }
        $.ajaxFileUpload({
            url:'/image/upload_tmp.php',
            secureuri:false,
            fileElementId:'avt_upload_file',
            dataType: 'json',
            data:{name:'logan', id:'id'},
            success: function (data, status)
            {
                if(data.code=='A00006')
                {
                    $('#avt_uploader').hide();
                    $('#avt_resize').show();

                    $('#avt_crop_valTmpname').val(data.data.name);
                    $('#avt_crop_show').attr('src','http://www.meila.com/image/show_tmp.php?name='+data.data.name);
                    $('#avt_crop_preview').attr('src','http://www.meila.com/image/show_tmp.php?name='+data.data.name);

                    console.log('img_w '+data.data.w);
                    if(data.data.w > data.data.h){
                        image_scale=600/data.data.w;
                        $('#avt_crop_show').css('width',600);
                        $('#avt_crop_preview').css('width',200);
                    }else{
                        image_scale=600/data.data.h;
                        $('#avt_crop_show').css('height',600);
                        $('#avt_crop_preview').css('height',200);
                    }
                    console.log('scale '+image_scale);

                    var cropX,cropY;

                    $('#avt_crop_show').Jcrop({
                        onChange: avt_showPreview,
                        onSelect: avt_showPreview,
                        aspectRatio: 1,
                        setSelect: [ 0, 0, 200, 200 ],
                        addClass: 'jcrop-dark',
                        allowMove:true
                    });


                }
            }
        })
    })


    function avt_showPreview(coords)
    {
        $('#avt_crop_valX').val(coords.x/image_scale);
        $('#avt_crop_valY').val(coords.y/image_scale);
        $('#avt_crop_valW').val(coords.w/image_scale);

        var rx = 200 / coords.w;
        var ry = 200 / coords.h;

        $('#avt_crop_preview').css({
            width: Math.round(rx * 600) + 'px',
            marginLeft: '-' + Math.round(rx * coords.x) + 'px',
            marginTop: '-' + Math.round(ry * coords.y) + 'px'
        });

    }

    $('#avt_crop_ok').click(function(){
        _post='tmp_name='+$('#avt_crop_valTmpname').val()
        +'&x='+$('#avt_crop_valX').val()
        +'&y='+$('#avt_crop_valY').val()
        +'&w='+$('#avt_crop_valW').val();
        $.post('/image/upload_portrait.php',_post , function(data){
            eval("data="+data);
            if(data.code=="A00006")
            {
                $('#avt_notice').text('保存成功');
            }
        });
    });

    $('#psn_nickinput').blur(function(){
        $.post('/riaapi/user/aj_checkNickAvailable.php','nick='+$(this).val() , function(data){
            eval("data="+data);
            if(data.code=="A00006")
            {
                $('#psn_nick').children().removeClass('error').addClass('success');
                $('#psn_nickinput').next('span').text('√');
            }
            else
            {
                $('#psn_nick').children().removeClass('success').addClass('error');
                $('#psn_nickinput').next('span').text(data.msg);
            }
        });
    });
    $('#psn_nicksubmit').click(function(){
        $.post('/riaapi/aj_personSetting.php','nickname='+$('#psn_nickinput').val() , function(data){
            eval("data="+data);
            if(data.code=="A00006")
            {
                $('#psn_nick').children().removeClass('error').addClass('success');
                $('#psn_nickinput').next('span').text('保存成功');
            }
            else
            {
                $('#psn_nick').children().removeClass('success').addClass('error');
                $('#psn_nickinput').next('span').text(data.msg);
            }
        });
    });
}